﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Configuration;
using System.Diagnostics;
using ajayumi.develop.win32.Transport.WCF.Services;

namespace ajayumi.develop.win32.Transport.AppUpgrade.Controls
{
    /// <summary>
    /// 更新
    /// Author   : 黄乙冬
    /// Date     : 2011-9-1 08:45:00
    /// </summary>
    public partial class UcUpgrading : UcBase
    {
        #region Variables

        //private AppFileHelper m_AppFileHelper = null;
        private UpgradeResultType m_UpgradeResult = UpgradeResultType.Failure;

        #endregion

        #region Properties

        /// <summary>
        /// 更新文件所在文件夹
        /// </summary>
        public string UpgradeDir { get; set; }

        #endregion

        public UcUpgrading()
        {
            InitializeComponent();
        }

        public UcUpgrading(UcBase preControl)
            : base(preControl)
        {
            InitializeComponent();
        }

        protected override void InitDefaultValue()
        {
            base.InitDefaultValue();

            this.Title = "程序升级";
        }

        internal override UcBase Next()
        {
            return new UcFinish(this) { UpgradeResult = this.m_UpgradeResult };
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            throw new NotSupportedException("该对象已经不在提供支持");

            this.SetButtonVisible(WizardFormButtons.Exit);

            switch (Program.UpgradeInfo.UpgradeRunMode)
            {
                case UpgradeRunMode.InstallationPackage:
                    string[] files = Directory.GetFiles(this.UpgradeDir, "*.exe", SearchOption.AllDirectories);
                    foreach (string item in files)
                    {
                        Process.Start(item);
                    }
                    System.Environment.Exit(System.Environment.ExitCode);
                    break;
                case UpgradeRunMode.Normal:
                case UpgradeRunMode.CompressionPackage:
                default:
                    //this.m_AppFileHelper = new AppFileHelper(ArgumentSerialization.AppDirectory);
                    //this.m_AppFileHelper.UpgradeProgressChanged += (sender, evt) =>
                    //{
                    //    this.backgroundWorker1.ReportProgress(evt.ProgressPercentage, evt.AppFileAction);
                    //};

                    this.backgroundWorker1.RunWorkerAsync();
                    break;
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                // 关闭待升级的应用程序
                Process[] processes = Process.GetProcessesByName(ArgumentSerialization.ProcessName);
                foreach (Process item in processes)
                {
                    MessageBox.Show("程序正在运行，请做好保存工作，然后关闭程序。升级操作将在程序关闭后继续运行。",
                        "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    item.WaitForExit();
                }

                //this.m_IsSuccess = this.m_AppFileHelper.UpgradeApp(this.UpgradeDir);
                if (true)
                {
                    ArgumentSerialization.Save();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }



        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            AppFileAction action = (AppFileAction)e.UserState;
            switch (action)
            {
                case AppFileAction.BackupFile:
                    this.lb.Text = "备份应用程序：";
                    break;
                case AppFileAction.UpdateFile:
                    this.lb.Text = "更新应用程序：";
                    break;
            }
            this.pgb.Value = e.ProgressPercentage;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.PerformNext();
        }
    }
}
